package com.tb;

import org.apache.commons.io.IOUtils;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 查找并下载 “小鸭一键下单“ 在的js
 *
 * @author zhao yuan
 * @since 2018/5/22
 */
public class XiaoYaJsCrawl {

    public static final String DIR = "C:\\Users\\huazh\\Documents\\work\\order\\xiaoya";

    public static void main(String[] args) throws IOException {
        File dir = new File(DIR);
        if(!dir.exists()) dir.mkdirs();
        Set<String> jsNames = findJSNames();
        for (String jsName : jsNames) {
            String jsUrl = MessageFormat.format("http://order.xiaoyataoke.com/v3/getscript.aspx?fileName={0}&fileType=js", jsName);
            Response response = Request.Get(jsUrl).execute();
            File file = new File(DIR,jsName + ".js");
            System.out.println(file.getAbsolutePath());
            response.saveContent(file);
        }
    }

    private static Set<String> findJSNames() throws IOException {
        InputStreamReader in = new InputStreamReader(SsdbImageAnalyze.class.getClassLoader().getResourceAsStream("main.js"),"utf-8");
        Pattern patter= Pattern.compile("getscript.aspx\\?fileName=([^&]+)&fileType=js");
        BufferedReader br = new BufferedReader(in);
        String content = IOUtils.toString(br);
        Matcher matcher = patter.matcher(content);
        Set<String> names = new HashSet<>();
        while (matcher.find()) {
            names.add(matcher.group(1));
        }
        return names;
    }
}
